package com.alvin.abp.common.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

/**
 * Global exception aspect.
 *
 */
@Aspect
public class ExceptionAspect {
	protected final Log logger = LogFactory.getLog(getClass());
    /**
     * A intercepter method to, the logic layer only catch the wrapped exception from dao layer,
     * after logged, throw it out to presentation layer to handle.
     * 
     * @param ex
     *            exception to catch
     */
    @AfterThrowing(pointcut="execution(* com.alvin.abp.service..*.*(..))", throwing="ex")
	public void afterServiceThrowing(BusinessException ex) {
    	StringWriter stringWriter = new StringWriter();
    	PrintWriter printWriter = new PrintWriter(stringWriter);  
    	ex.printStackTrace(printWriter);
    	logger.debug(stringWriter.toString());
    	// Logged it.
    	logger.error("After throw exception : "+ex.getMessage());
	}

}
